package top.guxx.springframework.beans.factory.support;

import top.guxx.springframework.beans.BeansException;
import top.guxx.springframework.beans.factory.config.BeanDefinition;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2024/6/8 21:24
 * @Filename：DefaultListableBeanFactory
 */
public class DefaultListableBeanFactory extends AbstractAutoWireCapableBeanFactory implements BeanDefinitionRegistry{
    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName,beanDefinition);
    }

    @Override
    public boolean containBeanDefinition(String beanName) {
        return  beanDefinitionMap.containsKey(beanName);
    }

    @Override
    protected BeanDefinition getBeanDefinition(String beanName) throws BeansException {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {throw new BeansException("No beans named '" + beanName + "' is defined");}
        return beanDefinition;
    }
}
